<?php

class IndexController extends Zend_Controller_Action
{

    public function init()
    {
        /* Initialize action controller here */
    }
    
    public function logoutAction()
    {
        Zend_Auth::getInstance()->clearIdentity();
        $this->_redirect('/');
    }

    public function indexAction()
    {
        
    }
    
    public function simpleAction()
    {
        $form = $this->view->form = new LoginForm();
        if ($this->getRequest()->isPost() 
            && $form->isValid($this->getRequest()->getPost())) {
            $adapter = new Zend_Auth_Adapter_DbTable(
                Zend_Db_Table_Abstract::getDefaultAdapter(),
                'users_v01',
                'email',
                'password'
            );
            $adapter->setIdentity($form->getValue('email'));
            $adapter->setCredential($form->getValue('password'));
            
            $result = Zend_Auth::getInstance()->authenticate($adapter);
            if (!$result->isValid()) {
                d($result->getMessages());
            } else {
                $this->_redirect('/');
            }
        }
    }
    
    
    
    public function hashAction()
    {
        $form = $this->view->form = new LoginForm();
        if ($this->getRequest()->isPost() 
            && $form->isValid($this->getRequest()->getPost())) {
            $adapter = new Zend_Auth_Adapter_DbTable(
                Zend_Db_Table_Abstract::getDefaultAdapter(),
                'users_v03',
                'email',
                'password',
                'SHA1(?)'
            );
            $adapter->setIdentity($form->getValue('email'));
            $adapter->setCredential($form->getValue('password'));
            
            $result = Zend_Auth::getInstance()->authenticate($adapter);
            if (!$result->isValid()) {
                d($result->getMessages());
            } else {
                $this->_redirect('/');
            }
        }
    }

    public function moreAction()
    {
        $form = $this->view->form = new LoginForm();
        if ($this->getRequest()->isPost() 
            && $form->isValid($this->getRequest()->getPost())) {
            $adapter = new Zend_Auth_Adapter_DbTable(
                Zend_Db_Table_Abstract::getDefaultAdapter(),
                'users_v03',
                'email',
                'password',
                'SHA1(?)'
            );
            $adapter->setIdentity($form->getValue('email'));
            $adapter->setCredential($form->getValue('password'));
            
            $result = Zend_Auth::getInstance()->authenticate($adapter);
            if (!$result->isValid()) {
                d($result->getMessages());
            } else {
                $storage = Zend_Auth::getInstance()->getStorage();
                $storage->write(
                    $adapter->getResultRowObject(
                        null, array('password')
                    )
                );
                $this->_redirect('/');
            }
        }
    }
    
    public function justLoggedAction()
    {
        
    }
}
